import axiosUtil from '../axios-util/axios-util';
import { ElNotification, ElMessageBox, ElMessage } from 'element-plus'
import { CloseBold } from '@element-plus/icons-vue'
const openNotificationWithIcon = (type: string, title: string, msg?: string) => {
    ElNotification({
        title: title,
        type: type as 'success' | 'warning' | 'info' | 'error',
        message: msg,
        closeIcon: CloseBold,
    })
};

const openMessageBoxWithIcon = (type: string, title: string, msg: string, callback?: (action: 'confirm' | 'cancel') => void) => {
    ElMessageBox.alert(msg, {
        title: title,
        type: type as 'success' | 'warning' | 'info' | 'error',
        confirmButtonText: '确定',
        callback: callback
    })
};

const openConfirmBox = (type: string, title: string, msg: string, confirm?: () => void, cancel?: () => void) => {
    ElMessageBox.confirm(msg, {
        title: title,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: type as 'success' | 'warning' | 'info' | 'error',
    }).then(() => {
        confirm && confirm();
    }).catch(() => {
        cancel && cancel();
    })
}

const openPromptBox = (title: string, msg: string, confirm?: (val) => void, cancel?: () => void) => {
    ElMessageBox.prompt(msg, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValidator: (val) => {
            if(val) {
                return true;
            }
            return false;
        }
    }).then(({ value }) => {
        confirm && confirm(value);
    }).catch(() => {
        cancel && cancel();
    })
}

const openTaskTitleBox = () => {
    return new Promise((resolve, reject) => {
        openPromptBox('创建任务', '请输入任务名称', (val) => {
            resolve(val);
        }, () => {
            reject();
        })
    })
}

const getUser = () => {
    if (sessionStorage.getItem('userInfo')) {
        return JSON.parse(sessionStorage.getItem('userInfo'))
    }
    return null;
}

const getProvinceList = () => {
    return  [
         '北京市',
         '天津市',
         '上海市',
         '重庆市',
         '河北省',
         '山西省',
         '辽宁省',
         '吉林省',
         '黑龙江省',
         '江苏省',
         '浙江省',
         '安徽省',
         '福建省',
         '江西省',
         '山东省',
         '河南省',
         '湖北省',
         '湖南省',
         '广东省',
         '海南省',
         '四川省',
         '贵州省',
         '云南省',
         '陕西省',
         '甘肃省',
         '青海省',
         '台湾省',
         '内蒙古自治区',
         '广西壮族自治区',
         '西藏自治区',
         '宁夏回族自治区',
         '新疆维吾尔自治区',
         '香港特别行政区',
         '澳门特别行政区'
        ];
}

const getTheme = () => {
    // 默认深色主题，后期可以从后台读取
    return 'dark';
}

let global_cache_menu = [];

export default {
    openNotification: openNotificationWithIcon,
    openMessageBox: openMessageBoxWithIcon,
    openConfirmBox: openConfirmBox,
    getUser: getUser,
    getProvinceList: getProvinceList,
    openPromptBox: openPromptBox,
    openTaskTitleBox: openTaskTitleBox,
    getTheme: getTheme,
    globalCacheMenu: global_cache_menu

}


